<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/20
 * Time: 9:44
 */
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\request;

/**
 *
 * 个人资料
 */
class Center extends Base{

    //
    public function index(){
     $ste=Db::name('users')->where('id='.Session::get('uid'))->find();
        $this->assign('ste',$ste);
        //最近一次登录的时间
//        $time=Db::name('login_record')->where('uid='.Session::get('uid').' and  operation=1')->order('id desc')->limit(0,1)->find();
        $time=Db::name('login_record')->where(['uid'=>Session::get('uid'),'operation'=>'login'])->order('id','desc')->find();
//        dump($time);die;
        $this->assign('time',$time);
        //提现的
        $str=Db::name('user_withdrawcash')->where('uid='.Session::GET('uid').' and state=1')->sum('amount');
        $strr=Db::name('user_withdrawcash')->where('uid='.Session::GET('uid').' and  state=2')->sum('amount');
        $this->assign('str',$str);
        //期货账户

        //此账号是否开户
        if ($ste['assetaccount'] !== '' and $ste['assetaccount'] != '00000') {
            $qjgjz = Db::name('futures_api')->where('state=1')->find();
            if($qjgjz['title']=='jt'){
            	 $qjgjz = Db::name('futures_api')->where('state=1')->find();
        	 $req['Action']='ReqQryAccountInfo';
                $req['ChdAccountID']=$ste['assetaccount'];
                $req['UserID']=$qjgjz['sa'];
                ksort($req);
                $url='';
                foreach ($req as $k=>$v){
                    $url=$url.$k.'='.$v.'&';
                }
                $req=rtrim($url,'&');
                //var_dump($req);
                $sign=md5($req.'&UserKey='.$qjgjz['sapass']);
                $req=$req.'&Sign='.$sign;
               //var_dump($req);
                $url=$qjgjz['api'].'?'.$req;
               // var_dump($url);
                $res=https_request($url);
                //var_dump($res);die;
                //var_dump(json_decode($res,true));die;
                $data=json_decode($res,true);
                if($data['ErrorID']==0){
                $data['Margin']=$data['LowAvailable']/100;
                $data['Balance']=$data['Available']/100;
        	    $this->assign('data',$data);
        	            
		        $this->assign('strr',$strr);
		        return $this->fetch('Personal/personal');
        	    }else{
        	    	return "<script>alert(".$data['ErrorMsg'].");window.location.href='".url('home/Deposit/deposit')."'</script>";
        	    }
            }else{
            header("Content-type: text/html; charset=utf-8");
            $requestid = 6;                                   //用户结果与请求统一的值
            // $sa='qjgj';                                     //期货管理员账号
            // $sapass='123';                                  //期货管理员密码
            $account = $ste['assetaccount'];                               //用户账号
            $url = $qjgjz['api'] . "queryaccount?requestid=" . $requestid . "&sa=" . $qjgjz['sa'] . "&sapass=" . $qjgjz['sapass'] . "&account=" . $account;
            //echo $url;
            $curl = curl_init(); // 启动一个CURL会话
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
            $tmpInfo = curl_exec($curl);     //返回api的json对象
            //关闭URL请求
            curl_close($curl);
            $arr = simplexml_load_string($tmpInfo);
            $xmljson = json_encode($arr);//将对象转换个JSON
            $xmlarray = json_decode($xmljson, true);//将json转换成数组
            $ar = $xmlarray['Result'];
            //var_dump($ar);die;
            $data = array();
            if(!isset($ar['Summary'])){
                return "<script>alert('期货账户有问题，请联系客服！');window.location.href='".url('home/Deposit/deposit')."'</script>";
            }
            $data = $ar['Summary'];

            //劣后资金
            $data['bad'] = $data['Balance'] - $data['Credit'];
            //var_dump($data['bad']);
           // var_dump($data);
            $this->assign('data',$data);
        

        $this->assign('strr',$strr);
        return $this->fetch('Personal/personal');

     }

    }
        $this->assign('strr',$strr);
        return $this->fetch('Personal/personal');
}
}